Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(storage): allow for Age int64* type and int64 type #6230

Merged
merged 10 commits into from
Jun 22, 2022

Conversation

frankyn
Copy link
Member

@frankyn frankyn commented Jun 21, 2022

Hi @codyoss and @tritone,

I added a helper func to handle different types (int64 and int64*) when reading data from Storage Apiary client.
I didn't add from Manual -> Apiary client because I need some help typecasting without Go yelling at me.

Towards unblocking: #6204

@product-auto-label product-auto-label bot added the size: s Pull request size is small. label Jun 21, 2022
@frankyn frankyn force-pushed the reflect-condition-field branch from a063185 to 5df45dc Compare June 22, 2022 00:01
@frankyn frankyn force-pushed the reflect-condition-field branch from 5df45dc to 8a9137c Compare June 22, 2022 00:58
@frankyn frankyn marked this pull request as ready for review June 22, 2022 00:59
@frankyn frankyn requested review from a team as code owners June 22, 2022 00:59
@frankyn frankyn changed the title fix: allow for pointer based types fix(storage): allow for Age int64* type and int64 type Jun 22, 2022
@product-auto-label product-auto-label bot added size: m Pull request size is medium. and removed size: s Pull request size is small. labels Jun 22, 2022
Copy link
Contributor

@tritone tritone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice, thanks for adding this Frank!

v := reflect.ValueOf(cond).Elem().FieldByName("Age")
if v.Kind() == reflect.Int64 {
return v.Interface().(int64)
} else if v.Kind() == reflect.Pointer {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pointer got added in a recent Go release; use reflect.Ptr here instead. (This is why the CI fails.)

@@ -1503,6 +1503,17 @@ func toCORSFromProto(rc []*storagepb.Bucket_Cors) []CORS {
return out
}

// Handle ptr or int64
func setAgeCondition(age int64, cond interface{}) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a note about why this is needed and a TODO/issue to remove in a future release.

@@ -1525,6 +1536,8 @@ func toRawLifecycle(l Lifecycle) *raw.BucketLifecycle {
},
}

setAgeCondition(r.Condition.AgeInDays, rr.Condition)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
setAgeCondition(r.Condition.AgeInDays, rr.Condition)
setAgeCondition(r.Condition.AgeInDays, &rr.Condition)

var tp testPointer
var want int64 = 10
setAgeCondition(want, &tp)
if getAgeCondition(&tp) != want {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if getAgeCondition(&tp) != want {
if getAgeCondition(tp) != want {

var ti testInt64
want = 100
setAgeCondition(100, &ti)
if getAgeCondition(&ti) != want {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if getAgeCondition(&ti) != want {
if getAgeCondition(ti) != want {

var want int64 = 10
setAgeCondition(want, &tp)
if getAgeCondition(&tp) != want {
t.Fatalf("got %v, want 10", *tp.Age)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
t.Fatalf("got %v, want 10", *tp.Age)
t.Fatalf("got %v, want %v", *tp.Age, want)

@@ -1503,6 +1503,17 @@ func toCORSFromProto(rc []*storagepb.Bucket_Cors) []CORS {
return out
}

// Handle ptr or int64
func setAgeCondition(age int64, cond interface{}) {
c := reflect.ValueOf(cond).Elem().FieldByName("Age")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would limit the amount of reflection needed by just passing in a pointer to the field directly -- do a recursive call if needed to go one more level in.

@@ -1595,6 +1608,19 @@ func toProtoLifecycle(l Lifecycle) *storagepb.Bucket_Lifecycle {
return &rl
}

// Handle ptr or int64
func getAgeCondition(cond interface{}) int64 {
v := reflect.ValueOf(cond).Elem().FieldByName("Age")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for this one, I would just pass in the field directly.

@codyoss
Copy link
Member

codyoss commented Jun 22, 2022

Some of my comments might not make sense if you refactor the getter/setter into working directly on fields FYI. Feel free to dismiss those

@codyoss
Copy link
Member

codyoss commented Jun 22, 2022

Also forgot to ask, how much of a hot path is this code on for normal operations? There is be a penalty for temporarily using reflection.

@product-auto-label product-auto-label bot added size: s Pull request size is small. and removed size: m Pull request size is medium. labels Jun 22, 2022
@frankyn
Copy link
Member Author

frankyn commented Jun 22, 2022

Also forgot to ask, how much of a hot path is this code on for normal operations? There is be a penalty for temporarily using reflection.

Anytime a customer gets Bucket.Attrs(), it will go through this path iff OLM policies are set on the bucket.
It will be a common path for customers but not as common as working with uploading or downloading objects.

@product-auto-label product-auto-label bot added size: m Pull request size is medium. and removed size: s Pull request size is small. labels Jun 22, 2022
@frankyn frankyn requested review from tritone and codyoss June 22, 2022 18:47
storage/bucket.go Outdated Show resolved Hide resolved
storage/bucket.go Outdated Show resolved Hide resolved
storage/bucket.go Outdated Show resolved Hide resolved
@frankyn frankyn requested a review from codyoss June 22, 2022 19:22
Copy link
Member

@codyoss codyoss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for all the changes!

@frankyn frankyn merged commit cc7acb8 into googleapis:main Jun 22, 2022
@frankyn frankyn deleted the reflect-condition-field branch June 22, 2022 20:01
tritone added a commit to googleapis/google-api-go-client that referenced this pull request Aug 17, 2022
Towards unblocking: googleapis/google-cloud-go#6230

Change Storage OLM Age condition type from int64 to *int64 to allow for existence of check

Co-authored-by: Chris Cotter <cjcotter@google.com>
@Albert-Gao
Copy link

Albert-Gao commented Sep 4, 2022

interesting, this PR "introduced" a breaking change... which breaks firebase.google.com/go/v4 v4.8.0
but I do not know why Golang installed the 1.22.1 of the this lib rather than 1.21 that firebase-admin-go depends on :D

image

@tritone
Copy link
Contributor

tritone commented Sep 6, 2022

interesting, this PR "introduced" a breaking change... which breaks firebase.google.com/go/v4 v4.8.0 but I do not know why Golang installed the 1.22.1 of the this lib rather than 1.21 that firebase-admin-go depends on :D

image

@Albert-Gao I believe the error you are seeing is from having incompatible versions of cloud.google.com/go/storage and google.golang.org/api. Also, I think it is an issue with your project's build rather than with firebase.google.com/go/v4 because the deps for the firebase package predate this change.

I would try upgrading both cloud.google.com/go/storage and google.golang.org/api to the most recent version, or downgrading google.golang.org/api to <= 0.93.0 if you need to stay on an older version of storage.

If this doesn't work, please file an issue rather than adding a PR comment here.

@Albert-Gao
Copy link

thanks, @tritone :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: m Pull request size is medium.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants